﻿#include "uimainwindow.h"
#include <QPainter>
#include <QColor>
#include <QTabWidget>
#include <QTabBar>

#include "navigationbar.h"
#include "homepage.h"
#include "runpage.h"

#include "../widget/biotabbar.h"

#include "setbiaoqu.h"
#include "setjinyangqi.h"

#include "maintainbiaoqu_lcts.h"
#include "maintainbiaoqu_sdkz.h"
#include "maintainbiaoqu_zsqjz.h"
#include "maintainjinyangqi_ndlts.h"
#include "maintainjinyangqi_sdkz.h"
#include "maintainjinyangqi_wzjz.h"
#include "maintainsysteminfo.h"

UiMainWindow::UiMainWindow(QWidget *parent)
    : QWidget(parent)
{
    uiInit();
}

UiMainWindow::~UiMainWindow() {}

void UiMainWindow::uiInit()
{
    this->setWindowFlag(Qt::FramelessWindowHint);
    this->setFixedSize(1920, 1080);

    m_navigationBar = new NavigationBar(this);
    connect(m_navigationBar, &NavigationBar::selectPageChange, this, &UiMainWindow::selectPageChange);
    m_navigationBar->resize(205, 1000);
    m_navigationBar->move(10, 70);

    m_view = new QStackedWidget(this);
    m_view->resize(1680, 1000);
    m_view->move(230, 70);

    m_home = new HomePage(m_view);
    m_home->setWindowFlag(Qt::WindowType::FramelessWindowHint);
    IconHelper* iconmgr = IconHelper::instance();
    QPixmap sePix = iconmgr->getPixmap(QColor("#2686ED"),IconHelper::FontAwesome6Solid,0xf5fd,25,26,26);
    QPixmap unsePix = iconmgr->getPixmap(QColor("#606266"),IconHelper::FontAwesome6Solid,0xf5fd,25,26,26);
    QPixmap unseFloatingPix = iconmgr->getPixmap(QColor("#606266"),IconHelper::FontAwesome6Solid,0xf5fd,28,30,30);
    m_navigationBar->addPage(m_home, sePix, unsePix, unseFloatingPix, "首页");

    m_run = new RunPage(m_view);
    sePix = iconmgr->getPixmap(QColor("#2686ED"),IconHelper::FontAwesome6Solid,0xf8c5,25,26,26);
    unsePix = iconmgr->getPixmap(QColor("#606266"),IconHelper::FontAwesome6Solid,0xf8c5,25,26,26);
    unseFloatingPix = iconmgr->getPixmap(QColor("#606266"),IconHelper::FontAwesome6Solid,0xf8c5,28,30,30);
    m_navigationBar->addPage(m_run, sePix, unsePix, unseFloatingPix, "批表运行");

    m_tabw_set = new BioTabWidget(false, m_view);
    m_set_biaoqu = new SetBiaoqu(m_tabw_set);
    m_tabw_set->addTab(m_set_biaoqu, "标曲模块");
    m_set_jinyangqi = new SetJinyangqi(m_tabw_set);
    m_tabw_set->addTab(m_set_jinyangqi, "进样器");
    sePix = iconmgr->getPixmap(QColor("#2686ED"),IconHelper::FontAwesome6Solid,0xf013,25,26,26);
    unsePix = iconmgr->getPixmap(QColor("#606266"),IconHelper::FontAwesome6Solid,0xf013,25,26,26);
    unseFloatingPix = iconmgr->getPixmap(QColor("#606266"),IconHelper::FontAwesome6Solid,0xf013,28,30,30);
    m_navigationBar->addPage(m_tabw_set, sePix, unsePix, unseFloatingPix, "设置");

    m_tabw_maintain = new BioTabWidget(false, m_view);
    m_tabw_maintain->setTabPosition(QTabWidget::North);
    m_tabw_maintain_bq = new BioTabWidget(true,m_tabw_maintain);
    m_tabw_maintain_bq->setTabPosition(QTabWidget::West);
    m_tabw_maintain_jyq = new BioTabWidget(true,m_tabw_maintain);
    m_tabw_maintain_jyq->setTabPosition(QTabWidget::West);

    m_maintain_bq_sdkz = new MaintainBiaoqu_SDKZ(m_tabw_maintain_bq);
    m_maintain_bq_lcts = new MaintainBiaoqu_LCTS(m_tabw_maintain_bq);
    m_maintain_bq_zsqjz = new MaintainBiaoqu_ZSQJZ(m_tabw_maintain_bq);

    m_maintain_jyq_ndlts = new MaintainJinYangQi_NDLTS(m_tabw_maintain_jyq);
    m_maintain_jyq_sdkz = new MaintainJinYangQi_SDKZ(m_tabw_maintain_jyq);
    m_maintain_jyq_wzjz = new MaintainJinYangQi_WZJZ(m_tabw_maintain_jyq);

    m_tabw_maintain_bq->addTab(m_maintain_bq_sdkz,"手动控制");
    m_tabw_maintain_bq->addTab(m_maintain_bq_lcts,"流程调试");
    m_tabw_maintain_bq->addTab(m_maintain_bq_zsqjz,"注射器校准");
    m_tabw_maintain->addTab(m_tabw_maintain_bq, "标曲模块");

    m_tabw_maintain_jyq->addTab(m_maintain_jyq_sdkz,"手动控制");
    m_tabw_maintain_jyq->addTab(m_maintain_jyq_wzjz,"位置校准");
    m_tabw_maintain_jyq->addTab(m_maintain_jyq_ndlts,"蠕动泵调试");
    m_tabw_maintain->addTab(m_tabw_maintain_jyq, "进样器模块");

    m_maintain_sys_info = new MaintainSystemInfo(m_tabw_maintain);
    m_tabw_maintain->addTab(m_maintain_sys_info, "系统信息");

    sePix = iconmgr->getPixmap(QColor("#2686ED"),IconHelper::FontAwesome6Solid,0xf7d9,25,26,26);
    unsePix = iconmgr->getPixmap(QColor("#606266"),IconHelper::FontAwesome6Solid,0xf7d9,25,26,26);
    unseFloatingPix = iconmgr->getPixmap(QColor("#606266"),IconHelper::FontAwesome6Solid,0xf7d9,28,30,30);
    m_navigationBar->addPage(m_tabw_maintain, sePix, unsePix, unseFloatingPix, "维护");

    m_navigationBar->startRun();
}

void UiMainWindow::selectPageChange(QString, QWidget *page) {
    bool exists = false;

    for (int i = 0; i < m_view->count(); ++i) {
        QWidget* currentPage = m_view->widget(i);
        if (currentPage == page) {
            exists = true;
            break;
        }
    }

    if (!exists)
        m_view->addWidget(page);

    m_view->setCurrentWidget(page);
    m_view->update();
}

void UiMainWindow::paintEvent(QPaintEvent *event)
{
    int m_titleHeight = 60;
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing, true);

    painter.save();
    painter.setBrush(QColor("#d9ecff"));
    painter.setPen(Qt::NoPen);
    painter.drawRect(rect());
    painter.restore();

    painter.save();
    painter.setBrush(QColor("#409EFF"));
    painter.setPen(Qt::NoPen);
    painter.drawRect(rect().x(), rect().y(), rect().width(), m_titleHeight);
    painter.restore();

    painter.save();
    painter.setPen(Qt::NoPen);
    QColor transparentColor(255, 255, 255);
    painter.setBrush(transparentColor);
    painter.drawRoundedRect(230, 70, 1680, 1000, 15, 15);
    painter.restore();
}
